/*******************************************************************/
/* @ All Rights Reserved Copyright (C) 2014 broada,Co.,Ltd.        */
/*                                                                 */
/*  NAME      = t_thrift_tbinary_req_analyze.c                     */
/*  NOTE      =                                                    */
/*  DATE      = 2017/02/20 by jikw                                 */
/*******************************************************************/

#include <CUnit/Basic.h>
#include <stdlib.h>

#include <thrift/comm/thrift_comm.h>
#include <thrift/tbinary/tbinary.h>
#include <thrift/thrift_core.h>

#include "frsutl.h"
#include "frcomm.h"
#include "frmem.h"
#include "conntrack.h"
#include "frct_comm.h"
#include "frreport_comm.h"
#include "frbyte_buffer.h"
#include "../../../../src/module/thrift/config.h"

static L7session_thrift_private* __thriftprivate_create(void)
{
    L7session_thrift_private *p_st_private = NULL;

    p_st_private = (L7session_thrift_private *)frmalloc(sizeof(L7session_thrift_private));
    if(p_st_private == NULL)
        return NULL;

    memset(p_st_private, 0x00, sizeof(L7session_thrift_private));

    frlist_header_init(&p_st_private->st_thrift_data_list);

    return p_st_private;

}

static void __list_report_node_free(Frlist_node *p_st_node)
{
    Frreport_data *p_st_report_data=NULL;
    if(p_st_node==NULL || p_st_node->data == NULL )
        return;

    p_st_report_data = (Frreport_data*)FRLIST_GET_NODE_DATA(p_st_node);
    if(p_st_report_data)
    {
        frreport_data_free(&p_st_report_data);
    }

    return;
}

void frthritf_list_node_free(Frlist_node *node);
extern int thrift_tbinary_req_analyze(Conntrack *p_st_conn
                                , Apm_packet *p_st_packet
                                , Frhash_header *p_st_hash_report
                                , L7_session *p_st_session);
extern int thrift_session_destroy(void *p_private_session);

static int __suite_init(void)
{

	return 0;
}

static int __suite_cleanup(void)
{

	return 0;
}

static void case1(void)
{   
    int i_ret = 0;
    Conntrack *p_st_conn = NULL;
    Apm_packet *p_st_data_in = NULL;
    Frhash_header *p_st_hash_report = NULL;
    L7_session *p_st_session = NULL;
    
    i_ret = thrift_tbinary_req_analyze(p_st_conn, p_st_data_in, p_st_hash_report, p_st_session);
    
    CU_ASSERT_EQUAL(i_ret, APM_L7_ANALYZE_FAIL);
    CU_ASSERT_PTR_EQUAL(p_st_session, NULL);

    return;
}

static void case2(void)
{   
    int i_ret = 0;
    Conntrack *p_st_conn = NULL;
    Apm_packet *p_st_data_in = NULL;
    Frhash_header *p_st_hash_report = NULL;
    L7_session *p_st_session = NULL;
    
    p_st_conn = (Conntrack *)frmalloc(sizeof(Conntrack));
    if(p_st_conn == NULL)
        goto ERROR;
    memset(p_st_conn, 0x00, sizeof(Conntrack));
    p_st_conn->flg_direct_known = FR_DIRECT_KNOWN;
    
    p_st_data_in = (Apm_packet *)frmalloc(sizeof(Apm_packet));
    if(p_st_data_in == NULL)
        goto ERROR;
    memset(p_st_data_in, 0x00, sizeof(Apm_packet));
    p_st_data_in->p_payload = (char *)"\x80\x01\x00\x01\x00\x00\x00\x12\x74\x72\x61\x6e\x73\x56\x61\x6c\x69\x64\x61\x74\x65\x3a\x69\x73\x4f\x6b\x00\x00\x00\x01\x00";
    p_st_data_in->ui_payload_len = 31;
    p_st_data_in->st_timestamp.tv_sec = 11;
    p_st_data_in->st_timestamp.tv_usec = 22;
    
    p_st_hash_report = frhash2_create2(2);
    CU_ASSERT_PTR_NOT_NULL(p_st_hash_report);
    
    p_st_session = (L7_session *)frmalloc(sizeof(L7_session));
    if(p_st_session == NULL)
        goto ERROR;
    memset(p_st_session, 0x00, sizeof(L7_session));
    p_st_session->p_session_private = __thriftprivate_create();
    if(p_st_session->p_session_private == NULL)
        goto ERROR;
    
    i_ret = thrift_tbinary_req_analyze(p_st_conn, p_st_data_in, p_st_hash_report, p_st_session);
    
    CU_ASSERT_EQUAL(i_ret, APM_L7_ANALYZE_SET_REQ(APM_L7_ANALYZE_SUCC));

    frfree(p_st_conn);
    frfree(p_st_data_in);
    frhash2_free2(p_st_hash_report, __list_report_node_free);
    thrift_session_destroy(p_st_session->p_session_private);
    frfree(p_st_session);

    return;
    
ERROR:
    if(p_st_conn)
        frfree(p_st_conn);
    if(p_st_data_in)
        frfree(p_st_data_in);
    if(p_st_hash_report)
        frhash2_free2(p_st_hash_report, __list_report_node_free);
    if(p_st_session)
    {
        if(p_st_session->p_session_private)
            frfree(p_st_session->p_session_private);
        frfree(p_st_session);
    }          

    return;    
}

static void case3(void)
{   
    int i_ret = 0;
    Conntrack *p_st_conn = NULL;
    Apm_packet *p_st_data_in = NULL;
    Frhash_header *p_st_hash_report = NULL;
    L7_session *p_st_session = NULL;
    
    p_st_conn = (Conntrack *)frmalloc(sizeof(Conntrack));
    if(p_st_conn == NULL)
        goto ERROR;
    memset(p_st_conn, 0x00, sizeof(Conntrack));
    p_st_conn->flg_direct_known = FR_DIRECT_KNOWN;
    
    p_st_data_in = (Apm_packet *)frmalloc(sizeof(Apm_packet));
    if(p_st_data_in == NULL)
        goto ERROR;
    memset(p_st_data_in, 0x00, sizeof(Apm_packet));
    p_st_data_in->p_payload = (char *)"\x80\x01\x00\x01\x00\x00\x00\x12\x74\x72\x61\x6e\x73\x56\x61\x6c\x69\x64\x61\x74\x65\x3a\x69\x73\x4f\x6b\x00\x00\x00\x01";
    p_st_data_in->ui_payload_len = 30;
    p_st_data_in->st_timestamp.tv_sec = 11;
    p_st_data_in->st_timestamp.tv_usec = 22;
    
    p_st_hash_report = (Frhash_header *)frmalloc(sizeof(Frhash_header));
    if(p_st_hash_report == NULL)
        goto ERROR;
    memset(p_st_hash_report, 0x00, sizeof(Frhash_header));
    
    p_st_session = (L7_session *)frmalloc(sizeof(L7_session));
    if(p_st_session == NULL)
        goto ERROR;
    memset(p_st_session, 0x00, sizeof(L7_session));
    p_st_session->p_session_private = __thriftprivate_create();
    if(p_st_session->p_session_private == NULL)
        goto ERROR;
    
    i_ret = thrift_tbinary_req_analyze(p_st_conn, p_st_data_in, p_st_hash_report, p_st_session);
    
    CU_ASSERT_EQUAL(i_ret, APM_L7_ANALYZE_TODO);
    CU_ASSERT_EQUAL(((L7session_thrift_private *)(p_st_session->p_session_private))->st_l4_trans.l_request_bytes, 30);
    CU_ASSERT_EQUAL(((L7session_thrift_private *)(p_st_session->p_session_private))->st_l4_trans.st_request_time_first_byte.tv_sec, 11);
    CU_ASSERT_EQUAL(((L7session_thrift_private *)(p_st_session->p_session_private))->st_l4_trans.st_request_time_first_byte.tv_usec, 22);
    CU_ASSERT_EQUAL(((L7session_thrift_private *)(p_st_session->p_session_private))->st_l4_trans.st_request_time.tv_sec, 0);
    CU_ASSERT_EQUAL(((L7session_thrift_private *)(p_st_session->p_session_private))->st_l4_trans.st_request_time.tv_usec, 0);

    frfree(p_st_conn);
    frfree(p_st_data_in);
    frfree(p_st_hash_report);
    thrift_session_destroy(p_st_session->p_session_private);
    frfree(p_st_session);

    return;
    
ERROR:
    if(p_st_conn)
        frfree(p_st_conn);
    if(p_st_data_in)
        frfree(p_st_data_in);
    if(p_st_hash_report)
        frfree(p_st_hash_report); 
    if(p_st_session)
    {
        if(p_st_session->p_session_private)
            frfree(p_st_session->p_session_private);
        frfree(p_st_session);
    }          

    return;    
}

static void case4(void)
{   
    int i_ret = 0;
    Conntrack *p_st_conn = NULL;
    Apm_packet *p_st_data_in = NULL;
    Frhash_header *p_st_hash_report = NULL;
    L7_session *p_st_session = NULL;
    L7session_thrift_private *p_st_private = NULL; 
    char *p_fr = (char *)"\x80\x01\x00\x01\x00\x00\x00\x16\x64\x69\x6d\x65\x6e\x73\x69\x6f\x6e\x3a\x67\x65\x74\x45\x72\x72\x6f\x72\x42\x79\x49\x64\x00\x00\x00\x01\x0b\x00\x01\x00\x00\x00\x06\x32\x30\x30\x36\x2e";
    
    p_st_conn = (Conntrack *)frmalloc(sizeof(Conntrack));
    if(p_st_conn == NULL)
        goto ERROR;
    memset(p_st_conn, 0x00, sizeof(Conntrack));
    p_st_conn->flg_direct_known = FR_DIRECT_KNOWN;
    
    p_st_data_in = (Apm_packet *)frmalloc(sizeof(Apm_packet));
    if(p_st_data_in == NULL)
        goto ERROR;
    memset(p_st_data_in, 0x00, sizeof(Apm_packet));
    p_st_data_in->p_payload = (char *)"\x30\x00";
    p_st_data_in->ui_payload_len = 2;
    p_st_data_in->st_timestamp.tv_sec = 12;
    p_st_data_in->st_timestamp.tv_usec = 23;
    
    p_st_hash_report = frhash2_create2(2);
    CU_ASSERT_PTR_NOT_NULL(p_st_hash_report);
    
    p_st_session = (L7_session *)frmalloc(sizeof(L7_session));
    if(p_st_session == NULL)
        goto ERROR;
    memset(p_st_session, 0x00, sizeof(L7_session));
    p_st_session->p_session_private = __thriftprivate_create();
    if(p_st_session->p_session_private == NULL)
        goto ERROR;
    
    p_st_private = (L7session_thrift_private*)p_st_session->p_session_private; 
    p_st_private->p_st_data = bytebuff_creat();
    if(bytebuff_add(p_st_private->p_st_data, p_fr, 46) != FR_SUCC)
        goto ERROR;
    ((L7session_thrift_private *)(p_st_session->p_session_private))->st_l4_trans.st_request_time_first_byte.tv_sec = 11;
    ((L7session_thrift_private *)(p_st_session->p_session_private))->st_l4_trans.st_request_time_first_byte.tv_usec = 22;
    ((L7session_thrift_private *)(p_st_session->p_session_private))->st_l4_trans.l_request_bytes = 193;
    
    i_ret = thrift_tbinary_req_analyze(p_st_conn, p_st_data_in, p_st_hash_report, p_st_session);
    
    CU_ASSERT_EQUAL(i_ret, APM_L7_ANALYZE_SET_REQ(APM_L7_ANALYZE_SUCC));

    frfree(p_st_conn);
    frfree(p_st_data_in);
    frhash2_free2(p_st_hash_report, __list_report_node_free);
    thrift_session_destroy(p_st_session->p_session_private);
    frfree(p_st_session);

    return;
    
ERROR:
    if(p_st_conn)
        frfree(p_st_conn);
    if(p_st_data_in)
        frfree(p_st_data_in);
    if(p_st_hash_report)
        frhash2_free2(p_st_hash_report, __list_report_node_free);
    if(p_st_session)
    {
        if(p_st_session->p_session_private)
            frfree(p_st_session->p_session_private);
        frfree(p_st_session);
    }          

    return;    
}

int add_suite_thrift_tbinary_req_analyze(void)
{
    CU_pSuite pSuite = NULL;

    if ((pSuite = CU_add_suite(MODULE_NAME" thrift_tbinary_req_analyze()", __suite_init, __suite_cleanup)) == NULL)
        return FR_FAIL;

    if (CU_add_test(pSuite, "case1", case1) == NULL ||
        CU_add_test(pSuite, "case2", case2) == NULL ||
        CU_add_test(pSuite, "case3", case3) == NULL ||
        CU_add_test(pSuite, "case4", case4) == NULL)
        return FR_FAIL;

    return FR_SUCC;
}
